# -*- coding: utf-8 -*-
# @Time    : 2024/2/20 22:15
# @Author  : 顾安
# @File    : 4.获取豆瓣电影名称数据.py
# @Software: PyCharm


import asyncio
from pyppeteer import launch


async def get_movies(page, url):
    await page.goto(url)
    await page.xpath('//div[@class="item"]')

    title_elements = await page.xpath('//div[@class="info"]//div[@class="hd"]/a/span[1]')

    # 临时保存所有的电影名称
    titles = list()

    for element in title_elements:
        title = await page.evaluate('(element) => element.textContent', element)
        titles.append(title.strip())

    next_page = await page.xpath('//span[@class="next"]/a')
    if next_page:
        next_page_url = await page.evaluate('(element) => element.href', next_page[0])
        titles.extend(await get_movies(page, next_page_url))  # 递归

    return titles


async def main():
    browser = await launch()
    page = await browser.newPage()

    try:
        url = 'https://movie.douban.com/top250'
        title_list = await get_movies(page, url)
        print(title_list, len(title_list))
    except Exception as e:
        print('访问异常:', e)
    finally:
        await browser.close()


asyncio.get_event_loop().run_until_complete(main())

